package gov.nist.applet.phone.security;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.Dimension;

public class EncryptDialog extends JDialog
{

	private static final long serialVersionUID = 1L;
	private JPanel jPanel = null;  //  @jve:decl-index=0:visual-constraint="99,22"
	private JLabel jlSrcFile = null;
	private JTextArea jtSrcFilePath = null;
	private JButton jButton = null;
	private JLabel jlDestFile = null;
	private JTextField jtDestFilePath = null;
	private JButton jButton1 = null;
	private JButton jbEncrypt = null;
	private JButton jbDecrypt = null;
	private JLabel jlKey = null;
	private JTextArea jtKey = null;
	private JPanel contentPane = null;
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel()
	{

		
		
		if (jPanel == null)
		{
			
			contentPane = new JPanel()
			{
				private static final long serialVersionUID = 1L;

				protected void paintComponent(Graphics g)
				{
					super.paintComponent(g);
					ImageIcon img = new ImageIcon(
							"images/chatDialog/chatDialog.png");
					g.drawImage(img.getImage(), 0, 0, null);
				};
			};
			jlKey = new JLabel();
			jlKey.setBounds(new Rectangle(39, 150, 71, 29));
			jlKey.setText("加密密钥");
			jlDestFile = new JLabel();
			jlDestFile.setBounds(new Rectangle(38, 98, 70, 26));
			jlDestFile.setText("解密文件");
			jlSrcFile = new JLabel();
			jlSrcFile.setText("加密文件");
			jlSrcFile.setBounds(new Rectangle(38, 32, 68, 33));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setSize(new Dimension(488, 322));
			jPanel.add(jlSrcFile, null);
			jPanel.add(getJtSrcFilePath(), null);
			jPanel.add(getJButton(), null);
			jPanel.add(jlDestFile, null);
			jPanel.add(getJtDestFilePath(), null);
			jPanel.add(getJButton1(), null);
			jPanel.add(getJbEncrypt(), null);
			jPanel.add(getJbDecrypt(), null);
			jPanel.add(jlKey, null);
			jPanel.add(getJtKey(), null);
		}
		contentPane.add(jPanel);
		contentPane.setLayout(null);
		contentPane.setBackground(Color.white);
		return contentPane;
	}

	/**
	 * This method initializes jtSrcFilePath	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJtSrcFilePath()
	{
		if (jtSrcFilePath == null)
		{
			jtSrcFilePath = new JTextArea();
			jtSrcFilePath.setBounds(new Rectangle(123, 36, 205, 35));
		}
		return jtSrcFilePath;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton()
	{
		if (jButton == null)
		{
			jButton = new JButton();
			jButton.setBounds(new Rectangle(337, 37, 73, 36));
			jButton.setText("浏览");
			jButton.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					    JFileChooser chooser = new JFileChooser(); 
					    chooser.setCurrentDirectory(new File(".")); 
					    int ret = chooser.showOpenDialog(null); 
					    if(ret==JFileChooser.APPROVE_OPTION){
					       EncryptDialog.this.jtSrcFilePath.setText(chooser.getSelectedFile().getPath()); 
					}
				}
			});
			jButton.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jtDestFilePath	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtDestFilePath()
	{
		if (jtDestFilePath == null)
		{
			jtDestFilePath = new JTextField();
			jtDestFilePath.setBounds(new Rectangle(124, 94, 205, 33));
		}
		return jtDestFilePath;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1()
	{
		if (jButton1 == null)
		{
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(334, 94, 76, 32));
			jButton1.setText("浏览");
			jButton1.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
				    JFileChooser chooser = new JFileChooser(); 
				    chooser.setCurrentDirectory(new File(".")); 
				    int ret = chooser.showOpenDialog(null); 
				    if(ret==JFileChooser.APPROVE_OPTION){
				       EncryptDialog.this.jtDestFilePath.setText(chooser.getSelectedFile().getPath()); 
				     } 
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jbEncrypt	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbEncrypt()
	{
		if (jbEncrypt == null)
		{
			jbEncrypt = new JButton();
			jbEncrypt.setBounds(new Rectangle(98, 207, 75, 33));
			jbEncrypt.setText("加密");
			jbEncrypt.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					String srcFilePath = EncryptDialog.this.jtSrcFilePath.getText();
					String destFilePath = EncryptDialog.this.jtDestFilePath.getText();
					String key = EncryptDialog.this.jtKey.getText();
					DESEncoder desEncoder = new DESEncoder(key);
					try
					{
						desEncoder.encryptFile(srcFilePath, destFilePath);
						 JOptionPane.showMessageDialog(null,"加密成功！","消息",JOptionPane.INFORMATION_MESSAGE);
					} catch (Exception e1)
					{
						e1.printStackTrace();
						JOptionPane.showMessageDialog(null,"加密失败！", "消息",JOptionPane.ERROR_MESSAGE);
						return;
					}
				}
			});
		}
		return jbEncrypt;
	}

	/**
	 * This method initializes jbDecrypt	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbDecrypt()
	{
		if (jbDecrypt == null)
		{
			jbDecrypt = new JButton();
			jbDecrypt.setBounds(new Rectangle(243, 209, 76, 34));
			jbDecrypt.setText("解密");
			jbDecrypt.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{

					String destFilePath = EncryptDialog.this.jtSrcFilePath.getText();
					String srcFilePath = EncryptDialog.this.jtDestFilePath.getText();
					String key = EncryptDialog.this.jtKey.getText();
					DESEncoder desEncoder = new DESEncoder(key);
					try
					{
						desEncoder.decryptFile(srcFilePath, destFilePath);
						 JOptionPane.showMessageDialog(null,"解密成功！","消息",JOptionPane.INFORMATION_MESSAGE);
					} catch (Exception e1)
					{
						e1.printStackTrace();
						JOptionPane.showMessageDialog(null,"解密失败！", "消息",JOptionPane.ERROR_MESSAGE);
						return;
					}
					
				}
			});
		}
		return jbDecrypt;
	}

	/**
	 * This method initializes jtKey	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJtKey()
	{
		if (jtKey == null)
		{
			jtKey = new JTextArea();
			jtKey.setBounds(new Rectangle(125, 149, 205, 30));
		}
		return jtKey;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		EncryptDialog dialog = new EncryptDialog(null);
		dialog.setVisible(true);
	}

	/**
	 * @param owner
	 */
	public EncryptDialog(Frame owner)
	{
		super(owner);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setBounds(new Rectangle(0, 0, 450, 347));
		this.setContentPane(getJPanel());
	}

}  //  @jve:decl-index=0:visual-constraint="174,11"
